/**
 * 
 */
package org.t2.design;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.gaming.design.loader.DesignParser;
import org.gaming.tool.GsonUtil;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

/**
 * 
 */
public class T2DesignParser implements DesignParser {

	public static final T2DesignParser parser = new T2DesignParser();
	
	@Override
	public Object parseList(String value, Class<?> clazz) {
		if(clazz == Integer.class || clazz == int.class) {
			return Collections.unmodifiableList(GsonUtil.getList(value, Integer.class));
			
		} else if(clazz == Long.class || clazz == long.class) {
			return Collections.unmodifiableList(GsonUtil.getList(value, Long.class));
			
		} else if(clazz == Float.class || clazz == float.class) {
			return Collections.unmodifiableList(GsonUtil.getList(value, Float.class));
			
		} else if(clazz == String.class) {
			return Collections.unmodifiableList(GsonUtil.getList(value, String.class));
		} else {
			JsonArray jsonArray = GsonUtil.parseArray(value);
			if(jsonArray == null) {
				return Collections.emptyList();
			}
			List<Object> list = new ArrayList<>(jsonArray.size());
			for(int i = 0; i < jsonArray.size(); i++) {
				JsonElement jele = jsonArray.get(i);
				list.add(parseStruct(jele.toString(), clazz));
			}
			return Collections.unmodifiableList(list);
		}
	}

	@Override
	public Object parseMap(String value, Class<?> keyClazz, Class<?> valueClazz) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object parseStruct(String value, Class<?> clazz) {
		try {
			Object struct = clazz.getConstructor().newInstance();
			if("".equals(value)) {
				return struct;
			}
			Field[] fields = clazz.getDeclaredFields();
			JsonArray jsonArray = GsonUtil.parseArray(value);
			
			for (int i = 0; i < fields.length && i < jsonArray.size(); i++) {
				String fieldValue = jsonArray.get(i).toString();
				
				Field field = fields[i];
				field.setAccessible(true);
				if(int.class.equals(field.getType()) || Integer.class.equals(field.getType())) {
					field.setInt(struct, Integer.parseInt(fieldValue));
					
				} else if(long.class.equals(field.getType()) || Long.class.equals(field.getType())) {
					field.setLong(struct, Long.parseLong(fieldValue));
					
				} else if(float.class.equals(field.getType()) || Float.class.equals(field.getType())) {
					field.setFloat(struct, Float.parseFloat(fieldValue));
					
				} else if(BigDecimal.class.equals(field.getType())) {
					field.set(struct, new BigDecimal(fieldValue));
					
				} else if(String.class.equals(field.getType())) {
					
					field.set(struct, fieldValue);
				} else {
					System.out.println("未处理的类型" + field.getType().getClass().getSimpleName());
				}
			}
			return struct;
		} catch (Exception e) {
			System.out.println("配置值：" + value + " 无法解释成：" + clazz.getSimpleName());
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
